#!/bin/sh

if [ -z "$ISOROOT_NAME" ]; then
  echo "You are not in an isoroot!"
  exit
fi

force=$PORT_TEST_FORCE
if [ -z "$force" ]; then force=0; fi

echo Sweeping...
rm -fr /usr/obj/*
/home/leeym/bin/pkg_sweep.sh > /dev/null

for x in $*; do
  echo $x | grep -q =
  if [ $? -eq 0 ]; then
    envs="$envs $x"
  else
    ports="$port $x"
  fi
done


echo ENV: $envs
echo PORTS: $ports

envs="$envs PATH=/usr/local/libexec/ccache:$PATH"

MAKE="env $envs make"
PORTSDIR="/usr/ports"
PACKAGES=`$MAKE -V PACKAGES -f $PORTSDIR/Mk/bsd.port.mk`

ln -sf /usr/local/bin/perl /usr/bin/perl

for port in $ports; do
  cd $port
  PORT=`pwd`
  PREFIX=`$MAKE -V PREFIX`
  WRKSRC=`$MAKE -V WRKSRC`
  PREFIX=`$MAKE -V PREFIX`
  PKGNAME=`$MAKE -V PKGNAME`
  DEPENDS=`$MAKE all-depends-list`
  SITE_PERL=`$MAKE -V SITE_PERL`
  PERL_ARCH=`$MAKE -V PERL_ARCH`
  USE_APACHE=`$MAKE -V USE_APACHE`
  APACHEETCDIR=`$MAKE -V APACHEETCDIR`
  PERL_MODBUILD=`$MAKE -V PERL_MODBUILD`
  USE_PERL5_RUN=`$MAKE -V USE_PERL5_RUN`
  PERL_CONFIGURE=`$MAKE -V PERL_CONFIGURE`
  TOOL_PORTS="devel/ccache"
  if [ ! -z "PERL_CONFIGURE" ]; then
    #TOOL_PORTS="$TOOL_PORTS devel/p5-Test-Simple devel/p5-Test-Pod devel/p5-Test-Pod-Coverage"
  fi
  for tool_port in $TOOL_PORTS; do
    for depend in $port $DEPENDS; do
      echo $depend | grep -q "$tool_port$"
      if [ $? -eq 0 ]; then
        NPKG="$NPKG $tool_port "
      fi
    done
  done
  for tool_port in $TOOL_PORTS; do
    echo "$NPKG" | grep -q "$tool_port "
    if [ $? -ne 0 ]; then
      PKG="$PKG $tool_port"
    fi
  done
  for tool_port in $PKG; do
    TOOL_PKG=`$MAKE -V PKGNAME -f $PORTSDIR/$tool_port/Makefile`
    TOOL_PKGS="$TOOL_PKGS $TOOL_PKG"
    if [ ! -d "/var/db/tool_port/$TOOL_PKG" ]; then
      if [ ! -f "$PACKAGES/All/$TOOL_PKG.tbz" ]; then
        cd $PORTSDIR/$tool_port && $MAKE package clean || exit 1
      else
        echo -n "pkg_add $TOOL_PKG ... "
        pkg_add $PACKAGES/All/$TOOL_PKG.tbz > /dev/null || exit 1
	echo DONE
      fi
    fi
  done

  # patch
  cd $PORT && $MAKE patch
  ERROR=$?; if [ $ERROR -ne 0 ]; then exit $ERROR; fi

  # configure
  if [ ! -z "$PERL_CONFIGURE" ]; then
    PREREQ_PM=`cd $PORT && $MAKE configure 2>&1 | grep "Warning: prerequisite"`
    if [ ! -z "$PREREQ_PM" ]; then
      echo "$PREREQ_PM"
      exit 1
    fi
  fi

  # build
  cd $PORT && $MAKE build
  ERROR=$?; if [ $ERROR -ne 0 ]; then exit $ERROR; fi

  # pre-install test
  if [ -z "$NO_TEST" -a ! -z "$PERL_CONFIGURE" ]; then
    $MAKE depends
    ERROR=$?; if [ $ERROR -ne 0 ]; then exit $ERROR; fi
    echo "===>  Testing for $PKGNAME"
    if [ ! -z "$PERL_MODBUILD" ]; then
      cd $WRKSRC && Build test
    else
      cd $WRKSRC && $MAKE test
    fi
  fi
  if [ $? -ne 0 -a $force -eq 0 ]; then exit $?; fi

  # install
  cd $PORT && $MAKE install
  ERROR=$?; if [ $ERROR -ne 0 ]; then exit $ERROR; fi

  # package
  cd $PORT && $MAKE package
  ERROR=$?; if [ $ERROR -ne 0 ]; then exit $ERROR; fi

  # post-install test
  if [ -z "$NO_TEST" -a ! -z "$PERL_CONFIGURE" ]; then
    echo "===>  Removing non-run-depends package(s)"
    /home/leeym/bin/pkg_sweep.sh $PKGNAME | sed 's/^/===>   /'
    STATUS=0
    echo "===>  Checking for $PKGNAME"
    for pm in `grep \.pm$ $port/pkg-plist | sed "s,%%SITE_PERL%%,$SITE_PERL,g;s,%%PERL_ARCH%%,$PERL_ARCH,g"`; do
      $PREFIX/bin/perl -cw $pm || STATUS=$?
    done
    for pl in `grep ^bin $port/pkg-plist`; do
      $PREFIX/bin/perl -cw $PREFIX/$pl || STATUS=$?
    done
    if [ $STATUS -ne 0 -a $force -eq 0 ]; then exit $STATUS; fi
  fi

  # deinstall, clean
  cd $PORT && $MAKE deinstall clean && if [ -d "$PKGDB/$TOOL_PKGS" ]; then pkg_delete $TOOL_PKGS; fi
  ERROR=$?; if [ $ERROR -ne 0 ]; then exit $ERROR; fi

  # sweep
  if [ ! -z "$USE_APACHE" ]; then
    echo "===>   Removing $PREFIX/$APACHEETCDIR/httpd.conf"
    rm -f $PREFIX/$APACHEETCDIR/httpd.conf
  fi
  /home/leeym/bin/pkg_sweep.sh
  ERROR=$?; if [ $ERROR -ne 0 ]; then exit $ERROR; fi
done
